<?php


namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use OSS\OssClient;
use App\Services\UploadService;
use App\Services\CommonService;
use App\Services\BaiduService;
use App\Models\Users;
use App\Http\Controllers\Controller;
use Log;

class UploadController extends Controller
{
    public function uploadAccessToken()
    {
        header('Access-Control-Allow-Origin: *'); 
        return UploadService::getAccessToken();
    }

    public function uploadBack(Request $request)
    {
        $authorizationBase64 = "";
        $pubKeyUrlBase64 = "";
        if (isset($_SERVER['HTTP_AUTHORIZATION'])) $authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION'];
        if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) $pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL'];
        if ($authorizationBase64 == '' || $pubKeyUrlBase64 == ''){
            header("http/1.1 403 Forbidden");
            exit();
        }

        // 2.获取OSS的签名
        $authorization = base64_decode($authorizationBase64);

        // 3.获取公钥
        $pubKeyUrl = base64_decode($pubKeyUrlBase64);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $pubKeyUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $pubKey = curl_exec($ch);
        if ($pubKey == ""){
            exit();
        }

        // 4.获取回调body
        $body = file_get_contents('php://input');

        // 5.拼接待签名字符串
        $authStr = '';
        $path = $_SERVER['REQUEST_URI'];
        $pos = strpos($path, '?');
        if ($pos === false) {
            $authStr = urldecode($path)."\n".$body;
        } else {
            $authStr = urldecode(substr($path, 0, $pos)).substr($path, $pos, strlen($path) - $pos)."\n".$body;
        }

        // 6.验证签名
        $ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5);
        if ($ok == 1)
        {
            header("Content-Type: application/json");
            $data = array("Status"=>"Ok");
            echo json_encode($data);
        } else {
            //header("http/1.1 403 Forbidden");
            exit();
        }
    }

    public function uploadCheck(Request $request)
    {
        // 小程序端上传后再审核图片
        $images = json_decode($request->images);
        $checkStatus = 1;
        $creditScore = Users::getUsersScode($request->users_id);
        $score = $creditScore->score;
        $left_score = $creditScore->score;
        foreach ($images as $image) {
            $baiducheck = BaiduService::baiduCheck($request->users_id,$image);
            if ($baiducheck != 1) {
                if ($baiducheck == 3) {
                    $left_score -= 3;
                    $checkStatus = 3;
                    break;
                }
                if ($creditScore->score <= 1) {
                    $checkStatus = 2;
                    break;
                } else {
                    // 扣除信用分
                    $left_score -= 1;
                }
            }
        }
        if ($checkStatus != 1) {
            if ($checkStatus == 2) {
                // 扣除信用分
                Users::decrementUsersScode($request->users_id,1);
                return response()->json(["status"=>"error","code"=>"1002","msg"=>"您上传的照片违规超过3次,账号已被封禁;如有疑问请联系企业微信客服！"]);
            } else {
                // 扣除信用分
                Users::decrementUsersScode($request->users_id,3);
                return response()->json(["status"=>"error","code"=>"1002","msg"=>"您上传的照片可能包含色情/暴力/政治,账号已被封禁;如有疑问请联系企业微信客服！"]);
            }
            // 禁用账户
            Users::updateDsiable($request->users_id,2);
        } else {
            if ($left_score <= 0) {
                // 扣除信用分
                Users::decrementUsersScode($request->users_id,1);
                // 禁用账户
                Users::updateDsiable($request->users_id,2);
                return response()->json(["status"=>"error","code"=>"1002","msg"=>"您上传的照片违规超过3次,账号已被封禁;如有疑问请联系企业微信客服！"]);
            } elseif ($left_score < $score) {
                // 扣除信用分
                Users::decrementUsersScode($request->users_id,($score-$left_score));
                return response()->json(["status"=>"error","code"=>"1001","msg"=>"您上传的照片存在违规请更换,如还违规 ".$left_score." 次将被封号！"]);
            }
        }
        return response()->json(["status"=>"success","code"=>"1000","msg"=>"图片审核成功！"]);
    }
}